/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import pojo.Loainv;
import util.HibernateUtil;

/**
 *
 * @author sangnguyen
 */
public class LoaiNhanVienDAO {
    public static boolean InsertLoainv(Loainv obj){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            session.save(obj);
            tx.commit();
        }
        catch(HibernateException ex){
            tx.rollback();
            System.err.println(ex);
            return false;
        }
        finally{
            session.close();
        }                                
        return true;
    }
    public static boolean UpdateLoainv(Loainv obj){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();        
        try{
            session.update(obj);
            tx.commit();
        }
        catch(HibernateException ex){
            tx.rollback();
            System.err.println(ex);
            return false;
        }
        finally{
            session.close();                            
        }
        return true;
    }
     public static boolean DeleteLoainv(Loainv obj){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            obj.setTinhtrang(0);
            session.save(obj);
            tx.commit();
        }
        catch(HibernateException ex){
            tx.rollback();
            System.out.println(ex);
            return false;
        }
        finally{        
            session.close();        
        }
        return true;
    }
     public static List<Loainv> SelectAllLoainv(){
         List<Loainv> lsLoainv = null;
         Session session = HibernateUtil.getSessionFactory().openSession();      
         try{
             Query query = session.createQuery("from Loainv l where l.tinhtrang=:tinhTrang order by l.id asc");
             query.setInteger("tinhTrang", 1);
             lsLoainv = query.list();         
         }
         catch(HibernateException ex){
             System.err.println(ex);                    
         }
         finally{
             session.close();        
         }         
         return lsLoainv;
     }
     public static Loainv GetLoainvID(int id){
         Loainv obj = new Loainv();
         Session session = HibernateUtil.getSessionFactory().openSession();         
         try{
             obj = (Loainv)session.get(Loainv.class, id);         
         }
         catch(HibernateException ex){
             System.err.println(ex);
         }
         finally{
            session.close();        
         }
         return obj;
     }
}
